package {{package}}.content;
import {{package}}.models.{{Model}};
import {{package}}.db.DatabaseHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class {{Model}}sDB extends ContentProvider {

private static final String DATABASE_NAME = "{{Model}}s";
private static final String {{Model}}S_TABLE = "{{Model}}s_table";
private static final int DATABASE_VERSION = 1;
public static final Uri CONTENT_URI = Uri.parse("content://{{package}}.{{Model}}sdb");
private static final UriMatcher uriMatcher;
{{#each fields}}
static final String {{FIELD}} = "{{field}}";
{{/each}}
static final String ID = "_id";

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public Cursor get{{Model}}s(){
    return db.query({{Model}}S_TABLE, new String[] {id{{#each fields}}, {{FIELD}}{{/each}}},null,null,null,null,"_id DESC");
}

public Cursor getId(String name){
    Cursor c = db.query({{Model}}S_TABLE, new String[] {{{#each fields}}, {{FIELD}} + "='" + {{field}} + "'"{{/each}}},null,null,null,null);
    if(c != null && c.moveToFirst()){
        do{
        	{{#each fields}}
        	{{#ifCond type 1}}
            if(c.getString(c.getColumnIndex({{FIELD}})).equals({{field}})){
                return c;
            }
            {{/ifCond}}
            {{#ifCond type 2}}
            if(c.getInteger(c.getColumnIndex({{FIELD}})).equals({{field}})){
                return c;
            }
            {{/ifCond}}
        }while(c.moveToNext());
    }
    return c;
}


@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    int count = 0;
    int num = uriMatcher.match(uri);
    if(num == 1){
        count = db.delete({{Model}}S_TABLE, selection,selectionArgs);
    }else if(num == 2){
        String id = uri.getPathSegments().get(1);
        count = db.delete({{Model}}S_TABLE, ID + " = " + id + (!TextUtils.isEmpty(selection) ? " AND (" + 
                   selection + ')' : ""), 
                   selectionArgs);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

@Override
public String getType(Uri uri) {
    if(uriMatcher.match(uri) == 1){
        return "vnd.android.cursor.dir/vnd.{{package}}.{{Model}}sdb ";
    }else if(uriMatcher.match(uri) == 2){
        return "vdn.android.cursor.item/vnd.{{package}}.{{Model}}sdb ";
    }else
        throw new IllegalArgumentException("Unsupported URI: " + uri);

}

@Override
public Uri insert(Uri uri, ContentValues values) {
    long rowID = db.insert({{Model}}S_TABLE,null, values);
    if(rowID > 0){
        Uri _uri = ContentUris.withAppendedId(CONTENT_URI,rowID);
        getContext().getContentResolver().notifyChange(_uri,null);
        return _uri;
    }else{
        throw new SQLException("Failed to insert row into " + uri);
    }
}

@Override
public boolean onCreate() {
    Context context = getContext();
    DBHelper = new DatabaseHelper(context);
    db = DBHelper.getWritableDatabase();
    return (db == null)? false:true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
    sqlBuilder.setTables({{Model}}S_TABLE);

    if(uriMatcher.match(uri) == 1){
        sqlBuilder.appendWhere(ID + " = " + uri.getPathSegments().get(1));
    }
    if(sortOrder == null || sortOrder == "")
        sortOrder = NAME;
    Cursor c = sqlBuilder.query(db, projection, selection, selectionArgs,null,null, sortOrder);
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    int count = 0;
    int num = uriMatcher.match(uri);
    if(num == 1){
        count = db.update({{Model}}S_TABLE, values, selection, selectionArgs);
    }else if(num == 2){
        count = db.update({{Model}}S_TABLE, values, ID + " = " + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? " AND (" + 
                  selection + ')' : ""), 
                  selectionArgs);
    }else{
        throw new IllegalArgumentException(
                "Unknown URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

static{
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI("{{package}}.{{Model}}sdb","{{Model}}s",1);
    uriMatcher.addURI("{{package}}.{{Model}}sdb","{{Model}}s/#",2);
}